/*
--------------------------------------------------------------------------------
				Ak-Lib. 
			 Project       	: Ak-Lib
			 Group         	: Ak-Lib R&D Team
			 Author        	: Ocean.Wang
			 Create Date 	: 05-01-2010
--------------------------------------------------------------------------------
Code Name 	:  A.K 	~v~
Description	:  Yet Another Cross-Platform Lib.
Cautions    	:
--------------------------------------------------------------------------------
Change History:
Version		: 0.1
Building 		: 05/01/10
10-05.01 first draft, build, debug and test.
--------------------------------------------------------------------------------
*/

#ifndef _INC_AK_OBJ_
#define	_INC_AK_OBJ_

#include "ak_types.h"
#include "ak_queue.h"
#include "ak_lock.h"
#include "ak_mem.h"
#include "ak_ie.h"
#include "ak_evt.h"

#ifdef __cplusplus
extern "C" {
#endif

/* The Common Object ID:  Object Path */
typedef struct{
	unsigned char 	bOff;		/* Bit Offset */
	unsigned char 	bCnt;		/* Bit Count */
}_t_sObjPath;

#define _SOBJ_PATH_MASK(pPath)				(~(0XFFFFFFFF << (pPath)->bCnt))
#define _SOBJ_PATH_ID_GET(vId, pPath) 		((vId >> (pPath)->bOff) & _SOBJ_PATH_MASK(pPath))
#define _SOBJ_PATH_ID_SET(vId, pPath) 		((vId  & _SOBJ_PATH_MASK(pPath)) << (pPath)->bOff)
#define _SOBJ_PATH_ID_SHADOW(vId, pPath) 	(vId & ( _SOBJ_PATH_MASK(pPath) << (pPath)->bOff))



/* The Common Object:
** 1. Object Hierarchy:
** Object could construct as a Tree, Master/Slave, Parent/Child, etc. 
** Our Object Model is designed to Serve the Event Transform & Life-Cycle Manage.
**
** 2. Object ID: 
** Each Object has an UUID, an unique ID. the Hierarchy is mapping to Object ID by the Object Path.
** Current 32-bit Used.
**
** 3. Object Type:
** Each Object has an Object Type, unique as to Diff, Serve the Event Classify.
**
** 4. Event Queue
** Each Object's Event is Sent to the Object Class's EventQ. That is, EventQ is Based on the Object Type.
** Multi-Type Object could Share the Same EventQ.
** Thus, When the Event is Raised and be Commu, the Type-Check could be done Auto.
*/


typedef struct{
	unsigned short 	classT;		/* Object Class Type : Current Only 16-bit is Used, for be Contained in the Event-Type !!*/
	unsigned short 	reserv;
	_t_sObjPath 	path;		/* Object ID Path Mapping 	*/
	const char 	*	name; 		/* Object Name */ 	
	
	/* Add Support for Event Table */
	unsigned int 			eCount;
	const unsigned int * 	eTable;		
	/* as a int Table[], be careful, the event should be ordered, 
	** The PreProcess will judge if an event is belong to a given table.
	** that is, between (table[0], table[cnt -1])
	*/
}_t_sObjDesc;

/* The Simple Marco to Define a Description */
#define TYPE_SOBJ_DESC(desc, bOff, bCnt, eCount, eTable) {desc, 0, {bOff, bCnt}, #desc, eCount, eTable}

typedef int 	(* 	funcSEvtHdl)(struct _t_sObject * pObj, _t_sEvent * pIn, _t_sEvent * pOut);

/* Object :
** 1. Each Object Could only be Create/Destroy By It's Parent !!!
** When Destroy, the Generate __FINI Event will be Post to It's Parent
**
** 2. Each Object will have a Class, which Binding to the Given Event Que, to Get Classified Events
** 
** 3. When Multicast, The Object should use the ObjSlot to Connect to the MC.
*/
typedef struct _t_sObject{
	/* Anchor, for Queue */
	_t_bnode 				node;
	
	/* State & Flag */
	int 					state;
	int 					flag;
	
	/* Each Object will contains a Parent Ptr. The Children is Maintain by App's Custom */
	struct _t_sObject 	* 	parent;
	_t_sObjDesc 		* 	desc;
	unsigned int 			id;		/* Object ID: Caution: 0 is Reserved! */

	/* the Object's Event Q & Handle, Each Object must Binding to an EvtQ */
	_t_sEvtQ 			* 	pEvtQ;
	funcSEvtHdl 			evtHdl;
}_t_sObject;


/* An Object Uses Obj Slot to Add itself into Different MC for cast Support */
typedef struct _t_sObjSlot{
	/* Anchor, for Queue */
	_t_bnode 				node;
	
	int 					flag;
	/* Back Reference : It's the Shadow to the Real Object. */
	_t_sObject 		* 		pObj;
}_t_sObjSlot;



#ifdef __cplusplus
}
#endif

#endif



